﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MM.Common.Message
{
    public class MessageData
    {
        // Fields
        public ClientInfo Client;
        public MessageCommand Command;
        public string MessageInfo;

        // Methods
        public MessageData()
        {
            this.Command = MessageCommand.Null;
            this.Client = new ClientInfo();
            this.MessageInfo = null;
        }

        public MessageData(byte[] data)
        {
            this.Command = MessageCommand.Null;
            this.Client = new ClientInfo();
            this.MessageInfo = null;
            this.Command = (MessageCommand)BitConverter.ToInt32(data, 0);
            int startIndex = 4;
            int count = BitConverter.ToInt32(data, startIndex);
            this.Client.ClientMessageID = int.Parse(Encoding.Default.GetString(data, startIndex + 8, count));
            startIndex = (startIndex + 8) + count;
            count = BitConverter.ToInt32(data, startIndex);
            if (count > 0)
            {
                this.Client.ClientIP = Encoding.Default.GetString(data, startIndex + 8, count);
            }
            else
            {
                this.Client.ClientIP = null;
            }
            startIndex = (startIndex + 8) + count;
            count = BitConverter.ToInt32(data, startIndex);
            if (count > 0)
            {
                this.Client.SystemCode = Encoding.Default.GetString(data, startIndex + 8, count);
            }
            else
            {
                this.Client.SystemCode = null;
            }
            startIndex = (startIndex + 8) + count;
            count = BitConverter.ToInt32(data, startIndex);
            this.Client.ClientID = int.Parse(Encoding.Default.GetString(data, startIndex + 8, count));
            startIndex = (startIndex + 8) + count;
            count = BitConverter.ToInt32(data, startIndex);
            if (count > 0)
            {
                this.Client.ClientName = Encoding.Default.GetString(data, startIndex + 8, count);
            }
            else
            {
                this.Client.ClientName = null;
            }
            startIndex = (startIndex + 8) + count;
            count = BitConverter.ToInt32(data, startIndex);
            if (count > 0)
            {
                this.MessageInfo = Encoding.Default.GetString(data, startIndex + 8, count);
            }
            else
            {
                this.MessageInfo = null;
            }
        }

        private void AppendString(List<byte> result, string str)
        {
            if (str == null)
            {
                result.AddRange(BitConverter.GetBytes(0));
                result.AddRange(BitConverter.GetBytes(0x7c));
            }
            else
            {
                result.AddRange(BitConverter.GetBytes(Encoding.Default.GetBytes(str).Length));
                result.AddRange(BitConverter.GetBytes(0x7c));
                result.AddRange(Encoding.Default.GetBytes(str));
            }
        }

        public void SetNull()
        {
            this.Client.SetNull();
            this.MessageInfo = null;
        }

        public byte[] ToByte()
        {
            List<byte> result = new List<byte>();
            result.AddRange(BitConverter.GetBytes((int)this.Command));
            this.AppendString(result, this.Client.ClientMessageID.ToString());
            this.AppendString(result, this.Client.ClientIP);
            this.AppendString(result, this.Client.SystemCode);
            this.AppendString(result, this.Client.ClientID.ToString());
            this.AppendString(result, this.Client.ClientName);
            this.AppendString(result, this.MessageInfo);
            return result.ToArray();
        }
    }
}
